<?php

namespace App\Http\Models;

use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\DB;

class SpaceOrder extends BaseMessage
{

    protected $table = 'tspaceorder';
    protected $fillable = ['SOAmount', 'SOUID', 'SOPayed', 'SOCustomerID','SOType'];
    protected $guarded = ['SOID'];
//    public static $rule = array(
//        'ftitle' => 'required',
//        'fbeginat' => 'required',
//        'fmessage' => 'required',
//        //'fspace' => 'numeric',
//        'fmobile' => 'required|numeric|min:0',
//        'flinkman' => 'required'
//    );
    public static function doCreate($data)
    {
        DB::beginTransaction();
        $data['SOCustomerID'] = Session::get('customer_id');
        $details = explode(",", trim($data['details'], ','));
        foreach ($details as $k => $v) {
            $temp = explode('-', $v);
            unset($details[$k]);
            $details[$k]['TODSpaceID'] = $temp[0];
            $details[$k]['TODBeginAt'] = $temp[1];
            $details[$k]['TODEndAt'] = $temp[2];
            $details[$k]['TODDate'] = $data['TODDate'];
        }
        //var_dump($temp);
        $STypeID = Space::where('SID', $temp[0])->first()->STypeID;
//        $datatype = json_decode(file_get_contents("http://www.easybots.cn/api/holiday.php?d=" . str_replace("-", "", $data['TODDate']))); //工作日0 休息日1 节假日2
//        foreach ($datatype as $value) {
//            $datatype = $value + 1; //工作日1 休息日2 节假日3
//        }
        $datatype = 1;
        $prices = SpaceOrder::getPrices($datatype, $STypeID, $data['SSiteID'], $data['basketball'], $data['soccer']);

        if ($datatype == 3) {

        } else if ($STypeID != 2) {
            $prices = DB::table('tcharge')->where('CSTID', $STypeID)->where('CPayTypeDay', $datatype)->where('CSiteID', $data['SSiteID'])->get();
        } else {
            $prices = DB::table('tcharge')->where('CSTID', $STypeID)->where('CPayTypeDay', $datatype)->where('CSiteID', $data['SSiteID'])->where('CDistinguish', $data['basketball'])->get();
        }
        $Space = new Space();
        $price = 0;
        foreach ($details as $k => $v) {
            $details[$k]['TODPrice'] = $Space->getPrice($v['TODBeginAt'] . '-' . $v['TODEndAt'], $prices);
            $price += $details[$k]['TODPrice'];
        }
        $model = new SpaceOrder();
        $data['SOAmount'] = $price;
        //扣除余额
        if($price>$data['amount']){
            return false;
        }
        User::where('CID',$data['SOCustomerID'])->update(array('CAmount'=>$data['amount']-$price));
        $data['SOType']=3;
        $order = $model->doSave($data);

        //var_dump($order->toArray());
        $Detail = new SpaceOrderDetail();
        $datas = SpaceOrderDetail::getDatas($data['TODDate']);
        foreach ($details as $k => $v) {
            $details[$k]['TODOID'] = $order->id;
            if (SpaceOrderDetail::isOrder($v['TODSpaceID'], $v['TODBeginAt'] . '-' . $v['TODEndAt'], $datas)) {
                if ($STypeID == 2 && $data['basketball'] == 2) {
                    if (!$Detail->doBasketballSave($details[$k], $datas)) {
                        DB::rollback();
                        return false;
                    } else {
                        //DB::commit();
                        //return true;
                    }
                } else {
                    $Detail->doSave($details[$k]);
                }
            } else {
                DB::rollback();
                return false;
            }

        }

        DB::commit();
        return true;

    }

    public static function getPrices($datatype, $STypeID, $SSiteID, $basketball, $soccer)
    {
        if ($STypeID == 3) {
            $prices = DB::table('tcharge')->where('CSTID', $STypeID)->where('CPayTypeDay', $datatype)->where('CSiteID', $SSiteID)->where('CDistinguish', $soccer)->get();
        } else if ($STypeID == 2) {
            $prices = DB::table('tcharge')->where('CSTID', $STypeID)->where('CPayTypeDay', $datatype)->where('CSiteID', $SSiteID)->where('CDistinguish', $basketball)->get();
        } else {
            $prices = DB::table('tcharge')->where('CSTID', $STypeID)->where('CPayTypeDay', $datatype)->where('CSiteID', $SSiteID)->get();
        }
        return $prices;
    }


}
